/*
This file is part of vlc-remote.

vlc-remote is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

vlc-remote is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with vlc-remote.  If not, see <http://www.gnu.org/licenses/>.
*/
import QtQuick 1.1
import com.meego 1.0
import "common"
import "mediaRemoteFunctions.js" as MediaRemote
import "remoteStatus.js" as RemoteStatus
import "vlcRemote.js" as VlcRemote

Page {
    id: root
    tools: commonTools
    property string server: ""
    property string serverName: "Not connected"
    property alias currentDir: browserModel.dir

    Component.onCompleted: {
        screen.allowedOrientations = Screen.Portrait;
        //lockInPortrait = true;
        orientationLock = Screen.Portrait;

        MediaRemote.init(server);
    }

    onServerChanged: {
        if ("" == server) {
            pageStack.push(remotesPage);
        }
        else {
            MediaRemote.init(server);
            console.debug("homeDir: " + homeDirectory);
            browserModel.dir = homeDirectory;
            browserModel.reload();
        }
    }

    onVisibleChanged: {
        //if (visible) browserModel.reload();
    }

    XmlListModel {
        id: browserModel
        property string dir:homeDirectory
        source: "http://"+server+"/requests/browse.xml?dir="+dir
        query: "/root/element"

        XmlRole { name: "name"; query: "@name/string()" }
        XmlRole { name: "extension"; query: "@extension/string()" }
        XmlRole { name: "path"; query: "@path/string()" }
        XmlRole { name: "type"; query: "@type/string()" }

        onStatusChanged: {
            //console.debug(source)
            if ( status == XmlListModel.Loading)
                busy.running = true;
            else  busy.running = false;

        }

    }

    ButtonStyle {
        id: vlcButtonStyle
        textColor: "white"
        background: "black"
    }


    PageHeader {
        id: pageHeader
        anchors.top: root.top
        title: qsTr("File browser")
        subtitle: serverName + " @ " + server + ""
        icon: "file:///usr/share/themes/blanco/meegotouch/icons/icon-m-toolbar-frequent-used-white.png"
        onClicked: {
            browserModel.dir = homeDirectory;
        }
    }

    ListView{
        id: browserListView
        anchors.fill: parent
        anchors.topMargin: 58
        anchors.bottomMargin: 64
        spacing: 4
        model:browserModel
        property int selection: -1
        delegate:ItemDelegate {
            //color: "black"
            //border.width: 0
           // border.color: accentColor
            title: "" + name + ""
            subTitle: "<i>" + path + "</i>"
            iconType: (type == "directory") ? "dir" :  MediaRemote.getListIconType(path);
            onClicked: {
                //console.debug(index + " " + browserListView.selection);
                browserListView.selection = index;
                browserListView.currentIndex = index;
            }
            onDoubleClicked: {
                if ( 0 == index || type == "directory")
                    browserModel.dir = path;
                else {
                    // add to playlist
                    MediaRemote.remoteRequestQueueByPath(path);
                    pageHeader.notify(name + qsTr(" added to playlist!"), 2000);
                    playlistDirty = true;
                }

            }
        }
        highlight: highlight
        highlightFollowsCurrentItem: true
        highlightMoveDuration: 2

        section.property: "name"
        section.criteria: ViewSection.FirstCharacter
        //section.delegate: sectionHeader

    }
    Component {
        id: highlight
        Rectangle {
            width: root.width
            //height: 56
            color: "black"
            border.width: 2
            border.color: accentColor
            radius:  8
            y: (-1 < browserListView.currentIndex) ? browserListView.currentItem.y : 0
            Behavior on y {
                SpringAnimation {
                    spring: 2
                    damping: 0.2
                }
            }
        }
    }
    Component {
        id: sectionHeader
        Rectangle {
            width: root.width
            height: childrenRect.height
            color: "black"
            border.width: 2
            border.color: accentColor
            Text {
                font.pixelSize: 24
                color: "white"
                font.bold: true
                text: section
            }
        }
    }

    ScrollDecoratorStyle {
        id: scrollDecoratorStyle
        inverted: true
    }
    ScrollDecorator {
        platformStyle: scrollDecoratorStyle
        flickableItem: browserListView
    }
/*
    SectionScrollerStyle {
        id: sectionScrollerStyle
        fontBoldProperty: true
        fontPixelSize: 24
        textColor: "white"
        textColorHighlighted: accentColor
    }
    SectionScroller {
        id: sectionScroller
        platformStyle: sectionScrollerStyle
        listView: browserListView
        visible: false;
    }
*/

    QueryDialog {
        id: dialogConfirmViewFavourites
        property alias message:  message.text
        titleText: qsTr("View favourites?")
        content:  Text {
            id: message
            width:  parent.width
            wrapMode: "WrapAtWordBoundaryOrAnywhere"
            maximumLineCount: 6
            font.pixelSize: 24
            color: "white"
            text: (-1 < browserListView.selection) ? qsTr("<b>") + browserModel.get(browserListView.selection).name + "</b> added. Do you want to view your favourites?" : ""
        }
        acceptButtonText: qsTr("Yes, view them")
        onAccepted: {
            if (-1 < browserListView.selection) {
                pageStack.push(favouritesPage);
            }
            load();
        }
        rejectButtonText: qsTr("No, stay here")
    }



    Rectangle {
        color: "black"
        width: parent.width
        height: controlsRow.height + 16
        anchors.bottom: root.bottom
        anchors.topMargin: 0
        anchors.bottomMargin: 0

    Row {
        id: controlsRow
        height: 64
        opacity: 100
        spacing: 4
        anchors.horizontalCenter: parent.horizontalCenter
        anchors.verticalCenter: parent.verticalCenter

        /*Button {
            //platformStyle: vlcButtonStyle
            id: btnUp
            enabled: true
            width: 86
            iconSource: "file:///usr/share/themes/blanco/meegotouch/icons/icon-m-toolbar-main-view-white.png"
            //text: "^"
            onClicked:  {
                browserModel.dir = (browserModel.get(0)) ? browserModel.get(0).path : "/../";
            }
        }*/
        Button {
            //platformStyle: vlcButtonStyle
            id: btnBrowse
            enabled: (-1 < browserListView.currentIndex)
            width: 86
            iconSource: (-1 < browserListView.currentIndex) ? "file:///usr/share/themes/blanco/meegotouch/icons/icon-m-toolbar-directory-white.png" : "file:///usr/share/themes/blanco/meegotouch/icons/icon-m-toolbar-directory-dimmed-white.png"
            //text: "->"
            onClicked:  {
                if (-1 < browserListView.currentIndex) browserModel.dir = browserModel.get(browserListView.currentIndex).path;
            }
        }
        Button {
            //platformStyle: vlcButtonStyle
            id: btnPlay
            enabled: (0 < browserListView.currentIndex)
            width: 86
            iconSource: (0 < browserListView.currentIndex) ? "file:///usr/share/themes/blanco/meegotouch/icons/icon-m-toolbar-mediacontrol-play-white.png" : "file:///usr/share/themes/blanco/meegotouch/icons/icon-m-toolbar-mediacontrol-play-dimmed-white.png"
            //text: "|>"
            onClicked:  {
                if (0 < browserListView.currentIndex) {
                    var name = browserModel.get(browserListView.currentIndex).name;
                    var path = browserModel.get(browserListView.currentIndex).path;
                    MediaRemote.remoteRequestTrackByPath(path);
                    playlistDirty = true;
                    pageHeader.notify(name + qsTr(" now playing!"), 2000);
                }
            }
        }
        Button {
            //platformStyle: vlcButtonStyle
            id: btnQueue
            enabled: (0 < browserListView.currentIndex)
            width: 86
            iconSource: (0 < browserListView.currentIndex) ? "file:///usr/share/themes/blanco/meegotouch/icons/icon-m-toolbar-content-audio-white.png" : "file:///usr/share/themes/blanco/meegotouch/icons/icon-m-toolbar-content-audio-dimmed-white.png"
            //text: "Q"
            onClicked:  {
                if (0 < browserListView.currentIndex) {
                    var name = browserModel.get(browserListView.currentIndex).name;
                    var path = browserModel.get(browserListView.currentIndex).path;
                    MediaRemote.remoteRequestQueueByPath(path);
                    playlistDirty = true;
                    pageHeader.notify(name + qsTr(" added to playlist!"), 2000);
                }
            }
        }
        Button {
            //platformStyle: vlcButtonStyle
            id: btnFavourite
            enabled: (0 < browserListView.currentIndex)
            width: 86
            iconSource: (0 < browserListView.currentIndex) ? "file:///usr/share/themes/blanco/meegotouch/icons/icon-m-toolbar-favorite-mark-white.png" : "file:///usr/share/themes/blanco/meegotouch/icons/icon-m-toolbar-favorite-mark-white.png"
            //text: "*"
            onClicked:  {
                if (0 < browserListView.currentIndex) {
                    var path = browserModel.get(browserListView.currentIndex).path;
                    var name = browserModel.get(browserListView.currentIndex).name;
                    MediaRemote.saveFavourite(name, serverName, path);
                    favouritesDirty = true;
                    pageHeader.notify(name + qsTr(" added to favourites!"), 2000);
                    //dialogConfirmViewFavourites.open();
                }
            }
        }
        Button {
            //platformStyle: vlcButtonStyle
            id: btnHomeDir
            enabled: (0 < browserListView.currentIndex)
            width: 86
            iconSource: (0 < browserListView.currentIndex) ? "file:///usr/share/themes/blanco/meegotouch/icons/icon-m-toolbar-frequent-used-white.png" : "file:///usr/share/themes/blanco/meegotouch/icons/icon-m-toolbar-frequent-used-dimmed-white.png"
            //text: "*"
            onClicked:  {
                if (0 < browserListView.currentIndex) {
                    var path = browserModel.get(browserListView.currentIndex).path;
                    var name = browserModel.get(browserListView.currentIndex).name;
                    MediaRemote.saveSetting("homeDirectory", serverName, "text", path, 0);
                    homeDirectory = path;
                    // need to give feedback
                    // set iconType = "heart" on current item delegate
                    //dialogConfirmViewFavourites.open();
                    pageHeader.notify(name + qsTr(" set as default directory!"), 2000);
                }
            }
        }
    }


}
    BusyIndicator{
        id:busy
        visible: running
        anchors.centerIn: browserListView
        running: true
        platformStyle: BusyIndicatorStyle { size: "large" }

    }
}
